package org.dfl.plugin.views;

import org.dfl.core.utils.Utils;
import org.dfl.messages.Messages;
import org.dfl.plugin.DflPlugin;
import org.eclipse.swt.graphics.Color;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleListener;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

/**
 * a console view that shows errors, warnings and infos in different colors
 * @author Piotr Wlodarczyk
 *
 */
public class DflPluginConsole extends MessageConsole implements IConsoleListener {
    private static final Color COLOR_INFO = new Color(null,0,0,0x80);
	private static final Color COLOR_WARNING = new Color(null,0xEF,0x8A,0x13);;
	private static final Color COLOR_NORMAL = new Color(null,0,0,0);
	private static final Color COLOR_ERROR = new Color(null,255,0,0);

	public DflPluginConsole() {
        super(
                Messages.DflPluginConsole_0,
                DflPlugin.getImageDescriptor("icons/dfl.gif")); //$NON-NLS-1$
        register();
    }

    public void consolesAdded(IConsole[] consoles) {
    }

    public void consolesRemoved(IConsole[] consoles) {
    }

    public static void outputError(String message) {
        outputMessage(message, COLOR_ERROR,true);
    }

    public static void outputWarning(String message) {
        outputMessage(message, COLOR_WARNING,false);
    }

    public static void outputInfo(String message) {
        outputMessage(message, COLOR_INFO,false);
    }

    public static void outputMessage(String message) {
        outputMessage(message, COLOR_NORMAL,false);
    }

    public static void outputMessage(String message, Color color,boolean bringToTop) {
   		Utils.execAsynch(new OutputMessageThread(message, color, bringToTop));    		
    }

    public void register() {
        IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
        IConsole[] existing = manager.getConsoles();
        boolean exists = false;

        for (int i = 0; i < existing.length; i++) {
            if (existing[i] == this) {
                exists = true;
                break;
            }
        }

        if (!exists) {
            manager.addConsoles(new IConsole[] { this });
        }
    }

    public void show() {
        IConsoleManager manager = ConsolePlugin.getDefault()
                .getConsoleManager();
        manager.showConsoleView(this);
    }

    public void unregister() {
        IConsoleManager manager = ConsolePlugin.getDefault()
                .getConsoleManager();
        manager.removeConsoles(new IConsole[] { this });
    }

    private static class OutputMessageThread implements Runnable {
        public void run() {
        	try{
	            DflPluginConsole console = DflPlugin.getDefault().getConsole();
	            MessageConsoleStream outputStream = console.newMessageStream();
	            outputStream.setColor(_color);
	            if(_show) console.show();
	            outputStream.print(_message); //$NON-NLS-1$
        	}catch(Throwable e){
        		e.printStackTrace();
        	}
        }

        OutputMessageThread(String message, Color color, boolean show) {
            _message = message;
            _color = color;
            _show = show;
        }

        private Color _color;
        private String _message;
        private boolean _show;
    }
}